import pytest
import allure
import requests
import json
import jsonpath
from core.globalContext import G_context

from extend.keyword import Keyword
g_context = G_context()
keyword = Keyword()
@pytest.fixture(scope="function")
def get_token():
    with allure.step("发送请求的数据"):
        url = 'http://shop-xo.hctestedu.com'
        params = {"s": "/api/user/login",
                  "application": "app"}
        data = {"accounts": "peipei_001", "pwd": "123456", "type": "username"}
    key_data = {'url': url,
                'params': params,
                'data': data}
    with allure.step("收到的相应数据"):
        keyword.request_post(**key_data)
        token = keyword.exp_jsonfile(EXVALUE='$..token', VMNAMW= 'token')
        print("返回的token值为:", token)
        return(token)

@pytest.fixture(scope="function")
def get_dict_token():
    url = 'http://shop-xo.hctestedu.com'
    params = {"s": "/api/user/login",
            "application": "app"}
    data = {"accounts": "peipei_001", "pwd": "123456", "type": "username"}
    key_data = {'url': url,
                'params': params,
                'data': data}
    res=keyword.request_post(**key_data)

    token = keyword.exp_jsonfile(EXVALUE='$..token',VARNAME = 'token')
    print ('token=========', token)
    G_context().set_dict('token', token)
    print (token)

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    # 通过 out = yield 定义了一个生成器。在生成器中，res = out.get_result() 获取了测试结果对象。
    out = yield
    res = out.get_result()
    #  res.when == "call"：表示正在运行调用测试函数的阶段。
    if res.when == "call":
        logging.info(f"用例ID：{res.nodeid}")
        logging.info(f"测试结果：{res.outcome}")
        logging.info(f"故障表示：{res.longrepr}")
        logging.info(f"异常：{call.excinfo}")
        logging.info(f"用例耗时：{res.duration}")
        logging.info("**************************************")

import datetime,logging,os
LOG_PATH = './logs'  # 请替换为你的实际路径
def pytest_configure(config):
    now = datetime.datetime.now()
    date_str = now.strftime("%Y-%m-%d_%H-%M-%S")  # 添加时间戳到日志文件名
    log_file_name = f'pytest_{date_str}.log'  # 使用带有时间戳的日志文件名
    # 构建完整的文件路径
    full_log_path = os.path.join(LOG_PATH, log_file_name)
    config.option.log_file = full_log_path
    config.option.log_file_mode = 'a'  # 设置日志文件以追加模式打开